﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionPedago.EntityContext;
using Persistence.OPManager;

namespace GestionPedago.Persistence.OPManager
{
    /// <summary>
    /// Manager de persistance de PlanningECF
    /// </summary>
    public abstract class PlanningECFOPManager
    {
        /// <summary>
        /// Méthode d'ajout d'un objet PlanningECF
        /// </summary>
        /// <param name="pe">L'objet PlanningECF à ajouter</param>
        /// <returns>L'objet PlanningECF</returns>
        public static PlanningECF AjouterPlanningECF(PlanningECF pe)
        {
            try
            {
                EntityConnection.ClearEntities();
                /*if (pe.Promotion != null)
                {
                    foreach (Cours c in pe.Promotion.Cours.ToList())
                    {
                        EntityConnection.GetContext().Cours.Detach(c);
                    }
                    EntityConnection.GetContext().Titre.Detach(pe.Promotion.Formation.Titre);
                    EntityConnection.GetContext().Formation.Detach(pe.Promotion.Formation);
                    EntityConnection.GetContext().Promotion.Detach(pe.Promotion);
                    pe.Promotion = null;
                }
                if (pe.ContactENI != null)
                {
                    EntityConnection.GetContext().ContactENI.Detach(pe.ContactENI);
                    pe.ContactENI = null;
                }
                if (pe.ECF != null)
                {
                    pe.ECF = ECFOPManager.GetECFById(pe.ECF.IdECF);
                    pe.IdECF = pe.ECF.IdECF;
                    if (!EntityStateChecker.IsDetached(pe.ECF))
                    {
                        EntityConnection.GetContext().ECF.Detach(pe.ECF);
                        pe.ECF = null;
                    }
                }
                
                if (!EntityStateChecker.IsDetached(pe))
                {
                    EntityConnection.GetContext().PlanningECF.Detach(pe);
                }*/
                pe.ContactENI = null;
                pe.ECF = null;
                pe.Promotion = null;

                EntityConnection.GetContext().PlanningECF.AddObject(pe);
                /*
                if (p != null)
                {
                    pe.Promotion = PromotionOPManager.GetPromotionByCode(p.CodePromotion);
                }*/

                EntityConnection.GetContext().SaveChanges();
                if (pe.CodePromotion != null)
                {
                    PlanningECFOPManager.AppliquerATousLesStagiairesPromotion(pe);
                }
                return pe;

            }
            catch (Exception ex)
            {

                throw ex;

            }
        }

        /// <summary>
        /// Méthode de récupération d'une liste de PlanningECF Hors promotion
        /// </summary>
        /// <returns>La liste de PlanningECF</returns>
        public static List<PlanningECF> GetListePlanningECFHorsPromotion()
        {
            List<PlanningECF> lst = new List<PlanningECF>();

            IQueryable<PlanningECF> query = EntityConnection.GetContext().PlanningECF;
            query = query.Where(it => it.CodePromotion == null);
            query = query.OrderBy(it => it.DateECF);

            List<PlanningECF> listePlanningECF = query.ToList();
            foreach (PlanningECF pe in listePlanningECF)
            {
                if (pe.CodePromotion == null)
                {
                    lst.Add(pe);
                }
            }

            return lst;
        }

        /// <summary>
        /// Méthode de récupération d'une liste de PlanningECF HorsPromotion par IdECF
        /// </summary>
        /// <param name="idECF">Identifiant de l'ECF</param>
        /// <returns>La liste de PlanningECF</returns>
        public static List<PlanningECF> GetListePlanningECFHorsPromotion(int idECF = 0)
        {

            List<PlanningECF> lst = new List<PlanningECF>();

            IQueryable<PlanningECF> query = EntityConnection.GetContext().PlanningECF;
            query = query.Where(it => it.CodePromotion == null && (idECF == 0 || it.IdECF == idECF));
            query = query.OrderBy(it => it.DateECF);

            List<PlanningECF> listePlanningECF = query.ToList();
            foreach (PlanningECF pe in listePlanningECF)
            {
                if (pe.CodePromotion == null)
                {
                    lst.Add(pe);
                }
            }

            return lst;
        }

        /// <summary>
        /// Méthode de récupération d'un PlanningECF par son identifiant
        /// </summary>
        /// <param name="code">Identifiant du PlanningECF</param>
        /// <returns>Le PlanningECF</returns>
        public static PlanningECF GetPlanningECFByCode(int code)
        {

            try
            {
                var query = from it in EntityConnection.GetContext().PlanningECF
                            where it.CodePlanningECF == code
                            select it;

                PlanningECF pe = null;

                pe = query.First();

                return pe;

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        /// <summary>
        /// Méthode de suppression des planning ECF Hors promotion isolés par Identifiant ECF
        /// </summary>
        /// <param name="idECF">Identifiant de l'ECF</param>
        public static void SupprimerTousPlanningECFHorsPromotionIsoles(int idECF)
        {
            foreach (PlanningECF pe in GetListePlanningECFHorsPromotion(idECF))
            {
                SupprimerPlanningECFHorsPromotionIsole(pe.CodePlanningECF);
            }
        }

        /// <summary>
        /// Méthode de suppression d'un PlanningECF
        /// </summary>
        /// <param name="codePlanningECF">Identifiant du planningECF</param>
        public static void SupprimerPlanningECFHorsPromotionIsole(int codePlanningECF)
        {
            PlanningECF pe = PlanningECFOPManager.GetPlanningECFByCode(codePlanningECF);
            if (pe.PlanningECFDetail.ToList().Count == 0)
            {
                SupprimerPlanningECF(pe);
            }
        }

        /// <summary>
        /// Méthode de suppression d'un planningECF
        /// </summary>
        /// <param name="pe">Le planningECF à supprimer</param>
        public static void SupprimerPlanningECF(PlanningECF pe)
        {
            try
            {
                List<PlanningECFDetail> lstASupprimer = PlanningECFDetailOPManager.GetListePlanningECFDetailByCodePlanningECF(pe.CodePlanningECF);

                foreach (PlanningECFDetail ped in lstASupprimer)
                {
                    EntityConnection.GetContext().PlanningECFDetail.DeleteObject(ped);
                    pe.PlanningECFDetail.Remove(ped);
                    EntityConnection.GetContext().PlanningECF.ApplyCurrentValues(pe);
                    EntityConnection.GetContext().SaveChanges();

                }
                EntityConnection.GetContext().PlanningECF.ApplyCurrentValues(pe);
                EntityConnection.GetContext().SaveChanges();
                EntityConnection.GetContext().PlanningECF.DeleteObject(PlanningECFOPManager.GetPlanningECFByCode(pe.CodePlanningECF));
                EntityConnection.GetContext().SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public static void AppliquerATousLesStagiairesPromotion(PlanningECF pe)
        {
            List<PlanningECFDetail> lstDejaModifies = new List<PlanningECFDetail>();
            if (pe.CodePromotion != null)
            {
                //Promotion p = PromotionOPManager.GetPromotionByCode(pe.CodePromotion);
                List<PlanningIndividuelFormation> listePif = PlanningIndividuelFormationOPManager.GetListePlanningIndividuelFormationByPromotion(pe.CodePromotion);
                foreach (PlanningIndividuelFormation pif in listePif)
                {
                    bool ecfAPlanifier = true;
                    List<PlanningECFDetail> listePed = PlanningECFDetailOPManager.GetListePlanningECFDetailByCodePlanningIndividuelFormation(pif.CodePlanning);
                    foreach (PlanningECFDetail ped in listePed)
                    {
                        if (ped.PlanningECF.IdECF == pe.IdECF)
                        {
                            ecfAPlanifier = false;
                            if (ped.PlanningECF.CodePlanningECF == pe.CodePlanningECF)
                            {
                                //ped.PlanningECF = pe;
                                ped.CodePlanningECF = pe.CodePlanningECF;
                                ped.CodeContactENI = pe.CodeContactENI;
                                //ped.ContactENI = pe.ContactENI;
                                ped.MiseAJourPourPlanificationParPromotion();
                                lstDejaModifies.Add(ped);
                            }
                        }
                    }
                    if (ecfAPlanifier)
                    {
                        PlanningECFDetail newPed = new PlanningECFDetail();
                        //EntityConnection.GetContext().PlanningECF.ApplyCurrentValues(this);
                        newPed.PlanningECF = pe;
                        //newPed.PlanningIndividuelFormation = pif;
                        //newPed.ContactENI = pe.ContactENI;
                        newPed.CodeContactENI = pe.CodeContactENI;
                        newPed.CodePlanningECF = pe.CodePlanningECF;
                        newPed.CodePlanning = pif.CodePlanning;
                        // EntityConnection.GetContext().PlanningECFDetail.Detach(newPed);
                        newPed = PlanningECFDetailOPManager.AjouterPlanningECFDetail(newPed, pe.IdECF,pe.CodePromotion, true);
                        lstDejaModifies.Add(newPed);
                    }


                }
            }

            List<PlanningECFDetail> listePed2 = PlanningECFDetailOPManager.GetListePlanningECFDetailByCodePlanningECF(pe.CodePlanningECF);
            foreach (PlanningECFDetail ped in listePed2)
            {

                if (ped.PlanningECF.IdECF == pe.IdECF)
                {
                    if (!lstDejaModifies.Contains(ped))
                    {
                        ped.CodePlanningECF = pe.CodePlanningECF;
                        ped.CodeContactENI = pe.CodeContactENI;
                        //ped.PlanningECF = pe;
                        //ped.ContactENI = pe.ContactENI;
                        ped.MiseAJourPourPlanificationParPromotion();
                        lstDejaModifies.Add(ped);
                    }
                }
            }
        }
    }
}
